# # -*- coding: utf-8 -*-
# import uuid
# from scrapy import Spider, Request
# from util.get_day import timestamp_for_ms
# from util.jiedian_base import JieDian
# from util.coordinate import GetCoordinate
# from items import jiedianPoiItem


# class JieDianPoi(Spider, JieDian):
#     name = 'jiedian_poi'
#     start_urls = []

#     All_Num=546049 # 目标抓取量（要保证是该时间最新关注量）
#     Save_Num=NumberCheck.find_save() # 已经抓取量
#     DB_Num=NumberCheck.find_db_real()  # 上次爬虫，数据库的最后一条数据DB_Num
#     Last_Num=NumberCheck.find_last()  # 获得上一次爬虫，轮子哥关注量


#     async def get_poi_list(self):
#         if self.mod == 'daily':
#             sql = f'SELECT `lat`, `lng` FROM jiedian_search_log WHERE shop_num>0 and `city`="{self.city}" and `crawl_version`<>{self.version}'
#             poi_list = await self.db.select(sql)
#             self.poi_list = GetCoordinate(poi_list)
#         else:
#             sql = f'''
#                 select a.`lat`, a.`lng`
#         from (
#             select `lat`, `lng`from `coordinate_detail_v2` where `city` = "{self.city}"
#              ) as a left join (
#             select `lat`, `lng` from `jiedian_search_log` where `city` = "{self.city}" and `crawl_version`={self.version}
#         ) as b on a.`lat` = b.`lat` and a.`lng` = b.`lng`
#         where b.`lng` is null
#                 '''
#             poi_list = await self.db.select(sql)
#             self.poi_list = GetCoordinate(poi_list, 1, 1)


#     def gen_req(self):
#         poi_list = self.poi_list.get()
#         if poi_list:
#             url = 'https://api.ankerjiedian.com/index.php'
#             headers = {
#                 'content-type': 'application/json; charset=UTF-8',
#                 'accept-encoding': 'gzip',
#                 'user-agent': 'okhttp/3.8.0'
#             }
#             lng = poi_list[0]
#             lat = poi_list[1]
#             t = timestamp_for_ms()
#             utoken = self.create_u_token(lng, lat, t)
#             session_authorization = self.create_session_authorization(f'{lng},{lat}')
#             data = {
#                 "body": {
#                     "gps_latitude": lat,
#                     "gps_longitude": lng,
#                     "latitude": lat,
#                     "longitude": lng
#                 },
#                 "header": {
#                     "access_token": "",
#                     "api": "YBusiness.getSimpleBusinessListByPosition",
#                     "applet": "android",
#                     "brand": "xiaomi",
#                     "client_v": "3.710",
#                     "dpi": "1080,1920",
#                     "model": "mi 5",
#                     "msg_id": str(uuid.uuid4()),
#                     "sensor_param": {
#                         "$referrer": "sharepower://page/home",
#                         "$url": "sharepower://page/home"
#                     },
#                     "service": "sharedCharging",
#                     "session_authorization": session_authorization,
#                     "session_id": str(uuid.uuid4()),
#                     "type": "",
#                     "ugen": str(uuid.uuid4()),
#                     "userAgent": "jiedian/ client_v/3.710 (MI 5; Android; Android OS ; 6.0.1; zh) ApacheHttpClient/4.0",
#                     "utime": t,
#                     "utoken": utoken
#                 },
#                 "isTokenIntercept": True
#             }
#             yield Request(url=url, headers=headers, callback=self.parse, method='post', json=data,
#                           text_check=['请求出错', 'Access Denied'], use_proxy=False, meta={'lat': lat, 'lng': lng})

#     def start_requests(self):
#         for i in range(100):
#             yield from self.gen_req()

#     def parse(self, response: Response):
#         poi_data = response.json
#         lat = response.meta['lat']
#         lng = response.meta['lng']
#         poi_list = poi_data['body']['business']
#         shop_num = len(poi_list)
#         data_list = []
#         for p in poi_list:
#             shop_lng = p['longitude']
#             shop_lat = p['latitude']
#             shop_id = p['uuid']
#             distance = p['distance']
#             data_list.append([lat, lng, shop_id, distance, shop_lat, shop_lng])
#         if data_list:
#             yield Item(data_list=data_list, table_name='jiedian_shop_poi', db=self.db,
#                        db_col=['search_lat', 'search_lng', 'shop_id', 'distance', 'shop_lat', 'shop_lng'],
#                        replace_str='"None"', insert_col=[],
#                        is_replace=True, unique_key=['shop_id'])
#         yield Item(data_list=[[lat, lng, shop_num, self.city, self.version]], table_name='jiedian_search_log',
#                    db=self.db, db_col=['lat', 'lng', 'shop_num', 'city', 'crawl_version'], replace_str='"None"',
#                    insert_col=[], is_replace=True, unique_key=['lat', 'lng'])
#         yield from self.gen_req()
